﻿using System;
using System.Data;
using System.Transactions;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IDAL;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Skyworth.CRM.DAL
{
    public class CustContactDAL : SuperDataBase, ICustContactDAL
    {
        public void Add(CustContact model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into T_BasicData_Contact(");
                strSql.Append("FCustId,FName,FIsD,FTel,FMobile,FEmail,FHobby,FDree,FNote)");
                strSql.Append(" values (");
                strSql.Append("@FCustId,@FName,@FIsD,@FTel,@FMobile,@FEmail,@FHobby,@FDree,@FNote)");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FCustId", DbType.Int32, model.FCustId);
                DB.AddInParameter(cmd, "@FName", DbType.String, model.FName);
                DB.AddInParameter(cmd, "@FIsD", DbType.Boolean, model.FIsD);
                DB.AddInParameter(cmd, "@FTel", DbType.String, model.FTel);
                DB.AddInParameter(cmd, "@FMobile", DbType.String, model.FMobile);
                DB.AddInParameter(cmd, "@FEmail", DbType.String, model.FEmail);
                DB.AddInParameter(cmd, "@FHobby", DbType.String, model.FHobby);
                DB.AddInParameter(cmd, "@FDree", DbType.String, model.FDree);
                DB.AddInParameter(cmd, "@FNote", DbType.String, model.FNote);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(CustContact model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_BasicData_Contact set ");
                strSql.Append("FCustId=@FCustId,");
                strSql.Append("FName=@FName,");
                strSql.Append("FIsD=@FIsD,");
                strSql.Append("FTel=@FTel,");
                strSql.Append("FMobile=@FMobile,");
                strSql.Append("FEmail=@FEmail,");
                strSql.Append("FHobby=@FHobby,");
                strSql.Append("FDree=@FDree,");
                strSql.Append("FNote=@FNote");
                strSql.Append(" where FItemId=@FItemId ");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FItemId", DbType.Int32, model.FItemId);
                DB.AddInParameter(cmd, "@FCustId", DbType.Int32, model.FCustId);
                DB.AddInParameter(cmd, "@FName", DbType.String, model.FName);
                DB.AddInParameter(cmd, "@FIsD", DbType.Boolean, model.FIsD);
                DB.AddInParameter(cmd, "@FTel", DbType.String, model.FTel);
                DB.AddInParameter(cmd, "@FMobile", DbType.String, model.FMobile);
                DB.AddInParameter(cmd, "@FEmail", DbType.String, model.FEmail);
                DB.AddInParameter(cmd, "@FHobby", DbType.String, model.FHobby);
                DB.AddInParameter(cmd, "@FDree", DbType.String, model.FDree);
                DB.AddInParameter(cmd, "@FNote", DbType.String, model.FNote);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int FItemId)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from T_BasicData_Contact ");
                strSql.Append(" where FItemId=@FItemId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FItemId", DbType.Int32, FItemId);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public CustContact GetModel(int fItemId)
        {
            try
            {
                CustContact model = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select  top 1 FItemId,FCustId,FName,FIsD,FTel,FMobile,FEmail,FHobby,FDree,FNote from T_BasicData_Contact ");
                strSql.Append(" where FItemId=@FItemId ");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FItemId", DbType.Int32, fItemId);
                using (IDataReader dr = DB.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        model = new CustContact();
                        if (dr["FItemId"].ToString() != "")
                        {
                            model.FItemId = int.Parse(dr["FItemId"].ToString());
                        }
                        if (dr["FCustId"].ToString() != "")
                        {
                            model.FCustId = int.Parse(dr["FCustId"].ToString());
                        }
                        model.FName = dr["FName"].ToString();
                        if (dr["FIsD"].ToString() != "")
                        {
                            if ((dr["FIsD"].ToString() == "1") || (dr["FIsD"].ToString().ToLower() == "true"))
                            {
                                model.FIsD = true;
                            }
                            else
                            {
                                model.FIsD = false;
                            }
                        }
                        model.FTel = dr["FTel"].ToString();
                        model.FMobile = dr["FMobile"].ToString();
                        model.FEmail = dr["FEmail"].ToString();
                        model.FHobby = dr["FHobby"].ToString();
                        model.FDree = dr["FDree"].ToString();
                        model.FNote = dr["FNote"].ToString();
                    }
                }
                return model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Query(string strWhere)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select FItemId,FCustId,FName,FIsD,FTel,FMobile,FEmail,FHobby,FDree,FNote ");
                strSql.Append(" FROM T_BasicData_Contact ");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(" where " + strWhere);
                }
                DataSet ds = DB.ExecuteDataSet(CommandType.Text, strSql.ToString());
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}